/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.db.mdm.hub;

import com.db.mdm.MDMAuditThread;
import com.db.mdm.MDMConnection;
import com.db.mdm.exception.MDMDBException;
import com.db.mdm.mssql.MSSQLConnection;
import com.db.mdm.mysql.MySQLConnection;
import com.db.mdm.postgresql.PostgresqlConnection;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.apache.commons.configuration.XMLConfiguration;

/**
 *
 * @author srkrishnan
 */
public class MDMHub implements Runnable {

    protected XMLConfiguration config;
    protected MDMConnection hubConnection;
    protected MDMConnection hubAuditConnection;
    protected MDMConnection repoConnection;

    /**
     * Get the value of repoConnection
     *
     * @return the value of repoConnection
     */
    public MDMConnection getRepoConnection() {
        return repoConnection;
    }

    /**
     * Set the value of repoConnection
     *
     * @param repoConnection new value of repoConnection
     */
    public void setRepoConnection(MDMConnection repoConnection) {
        this.repoConnection = repoConnection;
    }

    /**
     * Get the value of hubAuditConnection
     *
     * @return the value of hubAuditConnection
     */
    public MDMConnection getHubAuditConnection() {
        return hubAuditConnection;
    }

    /**
     * Set the value of hubAuditConnection
     *
     * @param hubAuditConnection new value of hubAuditConnection
     */
    public void setHubAuditConnection(MDMConnection hubAuditConnection) {
        this.hubAuditConnection = hubAuditConnection;
    }

    /**
     * Get the value of hubConnection
     *
     * @return the value of hubConnection
     */
    public MDMConnection getHubconnection() {
        return hubConnection;
    }

    /**
     * Set the value of hubConnection
     *
     * @param hubConnection new value of hubConnection
     */
    public void setHubconnection(MDMConnection hubconnection) {
        this.hubConnection = hubconnection;
    }

    /**
     * Get the value of config
     *
     * @return the value of config
     */
    public XMLConfiguration getConfig() {
        return config;
    }

    /**
     * Set the value of config
     *
     * @param config new value of config
     */
    public void setConfig(XMLConfiguration config) {
        this.config = config;
    }

    public MDMHub() {
    }

    public MDMHub(XMLConfiguration config, MDMConnection repoConnection) {
        this.config = config;
        this.repoConnection = repoConnection;
    }

    

    public void run() {

        /* Identify database type of hub and create connection*/
        if (config.getString("hub.type").equalsIgnoreCase("mysql")) {
            hubConnection = new MySQLConnection(config.getString("hub.name"),config.getString("hub.host"), config.getString("hub.user"), config.getString("hub.pass"), config.getString("hub.db"), config.getInt("hub.port"));
            hubConnection.connectDB();

            hubAuditConnection = new MySQLConnection(config.getString("hub.name"),config.getString("hub.host"), config.getString("hub.user"), config.getString("hub.pass"), config.getString("hub.audit.db"), config.getInt("hub.port"));
            hubAuditConnection.connectDB();

        }else if (config.getString("hub.type").equalsIgnoreCase("mssql")) {
            hubConnection = new MSSQLConnection(config.getString("hub.name"),config.getString("hub.host"), config.getString("hub.user"), config.getString("hub.pass"), config.getString("hub.db"), config.getInt("hub.port"));
            hubConnection.connectDB();

            hubAuditConnection = new MSSQLConnection(config.getString("hub.name"),config.getString("hub.host"), config.getString("hub.user"), config.getString("hub.pass"), config.getString("hub.audit.db"), config.getInt("hub.port"));
            hubAuditConnection.connectDB();

        } else {
            try {
                throw new MDMDBException("Database type not Supported.");
            } catch (MDMDBException ex) {
                ex.printStackTrace();
            }
        }

        /*Create audit tables in audit database*/
        hubConnection.createAuditTables(hubAuditConnection,repoConnection);

        /*Create Triggers for Insert,Update,Delete*/
        hubConnection.createTriggers(hubAuditConnection);

        
    }
}
